package com.visionblue.business.ship.modbus

import android.util.Log
import com.visionblue.business.ship.modbus.utilities.*
import com.visionblue.business.ship.model.bean.local.InstantFlowUnit
import com.visionblue.business.ship.model.bean.local.WaterFlowInfo
import java.util.*

/**
 * @author: xlh
 * @date: 2020-06-02 3:17 PM
 * @desc:
 */
class WaterInfoParser {

//    fun parse(results: IntArray, originDivisor: Int): WaterFlowInfo {
//        val waterFlowInfo = WaterFlowInfo()
//        val hexResultStr =
//            formatHex4Bits(results[0].toString(16)) + formatHex4Bits(results[1].toString(16)) +
//                    formatHex4Bits(results[2].toString(16)) + formatHex4Bits(results[3].toString(16))
//        val dividend = hexResultStr.toInt(16)
//
//        val finalDivisor = convertDivisor(originDivisor)
//
//        waterFlowInfo.divisor = originDivisor
//        waterFlowInfo.finalFlowRate = (dividend / finalDivisor).toDouble()
//
//        return waterFlowInfo
//    }

    private val accumulationUnitParser: IOilFlowAccumulationUnitParser =
        BOilFlowAccumulationUnitParserImpl()

    fun parse(results: IntArray): WaterFlowInfo {
        //Log.e("流量计-生活污水待解析数据1",results.toString())
//        Log.e("--->","开始解析")
//        Log.e("流量计-生活污水待解析数据2",Arrays.toString(results))
        val waterFlowInfo = WaterFlowInfo()

        // 瞬时流量 -
        waterFlowInfo.instantFlowRate = floatInverse(results, 0, 1)
//        waterFlowInfo.instantFlowRate = ieee754(results[3],results[4],results[5],results[6])

        // 瞬时流速
//        waterFlowInfo.instantFlowSpeed = floatInverse(results, 2, 3)
//        waterFlowInfo.instantFlowSpeed = floatInverse(results, 5, 6)

        // 流量百分比
//        waterFlowInfo.flowRatePercent = floatInverse(results, 4, 5)
//        waterFlowInfo.flowRatePercent = floatInverse(results, 7, 8)
        // 流体电导比
//        waterFlowInfo.flowElectronicRate = floatInverse(results, 6, 7)

        // 正向累计整数部分
        waterFlowInfo.positiveAccumulationIntPart = longInverse(results, 8, 9)
        // 正向累计小数部分
        waterFlowInfo.positiveAccumulationDecimalPart = floatInverse(results, 10, 11)

        // 正向累计流量：整数 + 小数
        waterFlowInfo.positiveAccumulationVal =
            waterFlowInfo.positiveAccumulationIntPart + waterFlowInfo.positiveAccumulationDecimalPart

        // 反向累计整数部分
        waterFlowInfo.negativeAccumulationIntPart =
            longInverse(results, 12, 13)

        // 反向累计小数部分
        waterFlowInfo.negativeAccumulationDecimalPart =
            floatInverse(results, 14, 15)

        // 瞬时流量单位
        waterFlowInfo.instantFlowUnit = InstantFlowUnit.fromKey(results[16])

        // 累计总量单位
        waterFlowInfo.accumulationFlowUnit =
            accumulationUnitParser.getAccumulationFlowUnit(results[17])

        // 上限报警
        waterFlowInfo.isUpperLimitWarning = results[18] and 1 == 1

        // 下线报警
        waterFlowInfo.isLowerLimitWarning = results[19] and 1 == 1

        // 空管报警
        waterFlowInfo.isEmptyWarning = results[20] and 1 == 1
        // 系统报警
        waterFlowInfo.isSystemWarning = results[21] and 1 == 1
        Log.e("流量计-解析结果：",waterFlowInfo.toString()+"qq")
        return waterFlowInfo
    }
}